<?php

require FCPATH . '/application/controllers/backend/backend.php';

class Project extends Backend {

	public function __construct() {
		parent::__construct();
		$this->load->model('project_model', 'model');
	}

	public function index() {
		$this->load->model('project_category_model');

		$data = array(
			'categories' => $this->project_category_model->records()
		);

		$this->load_js(array('jquery-ui-1.8.20.custom.min', 'backend_project'));
		$this->load_css(array('custom-theme/jquery-ui-1.8.20.custom', 'backend_project_index'));

		$this->rend('project_index', $data);
	}

	public function records() {
		$page = intval($this->input->get('page'));
		$page_size = intval($this->input->get('pageSize'));
		$page = $page ? $page : 1;
		$page_size = $page_size ? $page_size : 10;
		$offset = $page_size * ($page - 1);

		$conditions = array();
		$id = intval($this->input->get('id'));
		if (!empty($id)) {
			$conditions[] = array('where', 'id', $id);
		}

		$category = (int) $this->input->get('category');
		if ($category) {
			$conditions[] = array('like', 'category', ".$category.");
		}

		$name = trim($this->input->get('name'));
		if (!empty($name)) {
			$conditions[] = array('or_like', 'name_zh', $name);
			$conditions[] = array('or_like', 'name_fr', $name);
			$conditions[] = array('or_like', 'name_en', $name);
		}

		$labels = trim($this->input->get('labels'));
		if (!empty($labels)) {
			$conditions[] = array('or_like', 'labels_zh', $labels);
			$conditions[] = array('or_like', 'labels_fr', $labels);
			$conditions[] = array('or_like', 'labels_en', $labels);
		}

		$start = $this->input->get('start');
		if (!empty($start)) {
			$conditions[] = array('where', 'created_at >', $start);
		}

		$end = $this->input->get('end');
		if (!empty($end)) {
			$conditions[] = array('where', 'created_at <', $end);
		}

		$orders = array();
		$this->json_back('XT001', '', array(
			'total' => $this->model->total($conditions),
			'records' => $this->model->records(array(), $conditions, $orders, $page_size, $offset)
		));
	}

	public function add() {
		$this->load->model('project_category_model');
		$data = array(
			'categories' => $this->project_category_model->records()
		);
		$this->load_js(array('jquery-ui-1.8.20.custom.min', 'ImageBrowser', 'backend_project_form'));
		$this->load_css(array('custom-theme/jquery-ui-1.8.20.custom', 'backend_project_form'));
		$this->rend('project_form', $data);
	}

	public function insert() {
		$aCategories = $this->input->post('category');
		$sCategories = $aCategories ? ('.' . implode('.', $aCategories) . '.') : '';
		$data = array(
			'home_display' => intval($this->input->post('home_display')),
			'category' => $sCategories,
			'name_zh' => trim($this->input->post('name_zh')),
			'name_fr' => trim($this->input->post('name_fr')),
			'name_en' => trim($this->input->post('name_en')),
			'cover' => intval($this->input->post('cover')),
			'images' => trim($this->input->post('images')),
			'summary_zh' => $this->input->post('summary_zh'),
			'summary_fr' => $this->input->post('summary_fr'),
			'summary_en' => $this->input->post('summary_en'),
			'description_zh' => $this->input->post('description_zh'),
			'description_fr' => $this->input->post('description_fr'),
			'description_en' => $this->input->post('description_en'),
			'labels_zh' => trim($this->input->post('labels_zh')),
			'labels_fr' => trim($this->input->post('labels_fr')),
			'labels_en' => trim($this->input->post('labels_en'))
		);
		$this->validate($data);
		if (($id = $this->model->insert($data)) > 0) {
			$this->json_back('XT001', '新增项目成功', array('id' => $id));
		}
		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	public function edit() {
		$id = intval($this->uri->segment(4));
		$this->load->model('project_category_model');
		$data = array(
			'project' => $this->model->find($id),
			'categories' => $this->project_category_model->records()
		);
		$sCategories = $data['project']->category;
		if(strpos($sCategories, '.') === false){
			$aCategories = array();
		}else{
			$aCategories = explode('.', trim($sCategories, '.'));
		}
		$data['pcategories'] = $aCategories;
		$this->load_js(array('jquery-ui-1.8.20.custom.min', 'ImageBrowser', 'backend_project_form'));
		$this->load_css(array('custom-theme/jquery-ui-1.8.20.custom', 'backend_project_form'));
		$this->rend('project_form', $data);
	}

	public function update() {
		$id = intval($this->input->post('id'));
		$aCategories = $this->input->post('category');
		$sCategories = $aCategories ? ('.' . implode('.', $aCategories) . '.') : '';
		$data = array(
			'home_display' => intval($this->input->post('home_display')),
			'category' => $sCategories,
			'name_zh' => trim($this->input->post('name_zh')),
			'name_fr' => trim($this->input->post('name_fr')),
			'name_en' => trim($this->input->post('name_en')),
			'cover' => intval($this->input->post('cover')),
			'images' => trim($this->input->post('images')),
			'summary_zh' => $this->input->post('summary_zh'),
			'summary_fr' => $this->input->post('summary_fr'),
			'summary_en' => $this->input->post('summary_en'),
			'description_zh' => $this->input->post('description_zh'),
			'description_fr' => $this->input->post('description_fr'),
			'description_en' => $this->input->post('description_en'),
			'labels_zh' => trim($this->input->post('labels_zh')),
			'labels_fr' => trim($this->input->post('labels_fr')),
			'labels_en' => trim($this->input->post('labels_en')),
		);

		$this->validate($data);

		if ($this->model->update($id, $data)) {
			$this->json_back('XT001', '更新成功', array('id' => $id));
		}

		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	public function set_home_display() {
		$id = intval($this->uri->segment(4));
		$home_display = intval($this->uri->segment(5));
		$home_display = $home_display > 0 ? 1 : 0;
		if ($this->model->update($id, array('home_display' => $home_display))) {
			$this->json_back('XT001', '设置成功');
		}
		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	public function delete() {
		$id = intval($this->input->get('id'));

		if ($this->model->delete($id)) {
			$this->json_back('XT001', '删除成功。');
		}

		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	private function validate($data) {
		if (empty($data['name_zh'])) {
			$this->json_back('XT002', '请填写项目中文名称');
		}

		if (empty($data['name_fr'])) {
			$this->json_back('XT002', '请填写项目法文名称');
		}

		if (empty($data['name_en'])) {
			$this->json_back('XT002', '请填写项目英文名称');
		}

		if (empty($data['description_zh'])) {
			$this->json_back('XT002', '请填写项目中文描述');
		}

		if (empty($data['description_fr'])) {
			$this->json_back('XT002', '请填写项目法文描述');
		}

		if (empty($data['description_en'])) {
			$this->json_back('XT002', '请填写项目英文描述');
		}

		if (empty($data['labels_zh'])) {
			$this->json_back('XT002', '请填写项目中文标签');
		}

		if (empty($data['labels_fr'])) {
			$this->json_back('XT002', '请填写项目法文标签');
		}

		if (empty($data['labels_en'])) {
			$this->json_back('XT002', '请填写项目英文标签');
		}
	}

}
